package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.client.article.IArticleClient;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.UserRelationDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.user.ApUser;
import com.heima.model.common.pojos.user.ApUserFan;
import com.heima.model.common.pojos.user.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.ApUserService;
import com.heima.user.service.BehaviorUserService;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BehaviorUserServiceImpl extends ServiceImpl<ApUserFanMapper, ApUserFan> implements BehaviorUserService {

    @Autowired
    private ApUserService apUserService;

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private ApUserFanMapper apUserFanMapper;

    @Autowired
    private CacheService cacheService;



    /**
     * 用户关注与取消关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(UserRelationDto dto) {
        if (dto == null || dto.getAuthorId() == null || dto.getArticleId() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取用户id
        Integer userId = ApThreadLocalUtil.get();
        if (userId == null)throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        //获取数据
        String jsonStr = cacheService.get(String.valueOf(userId));
        Set<Integer> set = new HashSet<>();
        //判断是否存在数据
        if (StringUtils.isNotEmpty(jsonStr)) {
            //获取数据
            set = JSONObject.parseObject(jsonStr, Set.class);
        }
        ApUser author = apUserService.getById(Long.valueOf(dto.getAuthorId()));

        if (dto.getOperation() == 0) {
            //保存到用户关注表
            ApUser user = apUserService.getById(userId);
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(userId);
            apUserFollow.setFollowId(Long.valueOf(dto.getAuthorId()));
            apUserFollow.setFollowName(author.getName());
            apUserFollow.setLevel((short) 0);
            apUserFollow.setIsNotice((short) 0);
            apUserFollow.setCreatedTime(new Date());
            apUserFollowMapper.saveByUserId(apUserFollow);

            //保存到用户粉丝表
            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(author.getId());
            apUserFan.setFansId(userId);
            apUserFan.setFansName(user.getName());
            apUserFan.setCreatedTime(new Date());
            apUserFan.setLevel((short) 0);
            apUserFan.setIsDisplay((short) 0);
            apUserFan.setIsShieldLetter((short) 0);
            apUserFan.setIsShieldComment((short) 0);
            apUserFanMapper.insert(apUserFan);

            //关注作者
            set.add(dto.getAuthorId());
        } else if (dto.getOperation() == 1) {
            //删除用户关注表
            apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId,author.getId()));
            //删除用户粉丝表
            apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId,userId));
            //取消关注
            set.remove(dto.getAuthorId());

        }

        //存入redis
        cacheService.set(String.valueOf(userId), JSON.toJSONString(set));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
